import axios from "axios";
export interface ITranslatorService {
  translate(text: string, source: string, target: string): Promise<string>;
}

export class LingvaTranslationService implements ITranslatorService {
  api:string;
  constructor(api:string){
    this.api=api;
  }
  async translate(
    text: string,
    source: string,
    target: string
  ): Promise<string> {
    try {
      let result =await  axios.get(this.api+source+'/'+target+'/'+encodeURIComponent(text)+".json");
    return result.data.pageProps.translation;
    // do something with the data

    } catch (error: any) {
      throw error;
    }
  }
}

export class TranslationServiceFactory {
  static createServiceInstance(api: string): ITranslatorService {
   
      return new LingvaTranslationService(api);
    
  }
}
